<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\property\energy;

use app\common\repositories\property\basic\PropertyBasicRepository;
use think\App;
use app\controller\property\Base;
use app\validate\energy\EnergyPlanValidate;
use app\common\repositories\energy\EnergyPlanRepository;


class EnergyPlan extends Base
{
    protected $repository;

    public function __construct(App $app, EnergyPlanRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        $this->assign([
            'addAuth' => property_auth('propertyEnergyPlanAdd'),
            'editAuth' => property_auth('propertyEnergyPlanEdit'),
            'delAuth' => property_auth('propertyEnergyPlanDel'),
            'detailAuth' => property_auth('propertyEnergyPlanDetail'),
        ]);
    }

    public function list()
    {
        if ($this->request->isAjax()) {
            $where = $this->request->param([
                'property_id' => '',
                'keywords' => '',
            ]);
            [$page, $limit] = $this->getPage();
            $data = $this->repository->getpropertyList($where, $page, $limit, $this->request->propertyId);
            return json()->data(['code' => 0, 'data' => $data['list'], 'count' => $data['count']]);
        }
        return $this->fetch('energy/plan/list');
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $param = $this->paramValues();
            validate(EnergyPlanValidate::class)->scene('add')->check($param);
            if ($this->repository->propertyFieldExists($this->request->propertyId, 'plan_name', $param['plan_name'])) {
                return $this->error(' 已存在 ' . $param['plan_name'] . ' 方案');
            }
            try {
                $param['add_uid'] = $this->request->userId;
                $res = $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId);
                if ($res) {
                    return $this->success('添加成功');
                } else {
                    return $this->error('添加失败');
                }
            } catch (\Exception $e) {
                return $this->error('网络错误');
            }
        } else {
            $this->commonParams();
            return $this->fetch('energy/plan/add');
        }

    }

    protected function paramValues()
    {
        $param = $this->request->param([
            'plan_name' => '',
            'mode_type' => '',
            'stair_price' => [],
            'basic_price' => '',
            'basic_price_j' => '',
            'basic_price_f' => '',
            'basic_price_p' => '',
            'basic_price_g' => '',
            'service_switch' => '',
            'service_name' => '',
            'service_price' => '',
            'service_price_j' => '',
            'service_price_f' => '',
            'service_price_p' => '',
            'service_price_g' => '',
            'loss_switch' => '',
            'loss_name' => '',
            'loss_per' => '',
            'is_show' => '',
            'remark' => ''
        ]);
        switch ($param['mode_type']) {
            case 1:
                $param['basic_price'] = $param['basic_price'];
                $param['stair_price'] = [];
                break;
            case 2:
                $param['basic_price'] = '';
                $param['stair_price'] = [];
                $param['basic_price_j'] = $param['basic_price_j'];
                $param['basic_price_f'] = $param['basic_price_f'];
                $param['basic_price_p'] = $param['basic_price_p'];
                $param['basic_price_g'] = $param['basic_price_g'];
                break;
            case 3:
                $param['basic_price'] = '';
                $param['stair_price'] = $param['stair_price'];
                break;
            default:
                return $this->error('方案类型错误');
                break;
        }
        return $param;
    }

    public function edit()
    {
        $id = (int)$this->request->param('id');
        if (!$id) {
            return $this->error('参数错误');
        }
        $info = $this->repository->get($id);
        if (!$info) {
            return $this->error('信息错误');
        }
        if ($this->request->isPost()) {
            $param = $this->paramValues();
            validate(EnergyPlanValidate::class)->scene('edit')->check($param);
            if ($this->repository->propertyFieldExists($this->request->propertyId, 'plan_name', $param['plan_name'], $id)) {
                return $this->error(' 已存在 ' . $param['plan_name'] . ' 方案');
            }
            try {
                $res = $this->repository->editInfo($info, $param, $this->request->userId);
                if ($res) {
                    return $this->success('修改成功');
                } else {
                    return $this->error('修改失败');
                }
            } catch (\Exception $e) {
                return $this->error('网络错误');
            }
        } else {
            $this->commonParams();
            return $this->fetch('energy/plan/edit', [
                'info' => $info,
            ]);
        }
    }


    public function detail()
    {
        $planId = (int)$this->request->param('plan_id');
        if ($planId <= 0) {
            return $this->error('参数错误');
        }
        $info = $this->repository->getDetail($planId);
        if ($info['id'] <= 0) {
            return $this->error('方案不存在');
        }
        return $this->fetch('energy/plan/detail', [
            'info' => $info
        ]);
    }


    public function del()
    {
        $ids = (array)$this->request->param('ids');
        try {
            $data = $this->repository->batchDelete($ids, $this->request->userId);
            if ($data) {
                return $this->success('删除成功');
            } else {
                return $this->error('删除失败');
            }
        } catch (\Exception $e) {
            return $this->error('网络失败');
        }
    }


}